package at.ac.tuwien.swa.sparrow.server.conf

import at.ac.tuwien.swa.sparrow.server.security.web.SecurityFilter
import javax.servlet.ServletContext
import org.springframework.web.WebApplicationInitializer
import org.springframework.web.context.ContextLoaderListener
import org.springframework.web.context.support.{HttpRequestHandlerServlet, AnnotationConfigWebApplicationContext}
import org.springframework.web.servlet.DispatcherServlet

class WebAppInitializer extends WebApplicationInitializer {

	override def onStartup(servletContext: ServletContext) {
		val appContext = new AnnotationConfigWebApplicationContext
		appContext.setServletContext(servletContext)
		appContext.register(classOf[SpringConfig])
		appContext.refresh()


		appContext.getAutowireCapableBeanFactory autowireBean SecurityFilter


		val listener = new ContextLoaderListener(appContext)
		servletContext.addListener(listener)

		val dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(appContext))
		dispatcher.setLoadOnStartup(1)
		dispatcher.addMapping("/service/*")

		val clientServlet = servletContext.addServlet("clientService", classOf[HttpRequestHandlerServlet])
		clientServlet.setLoadOnStartup(1)
		clientServlet.addMapping("/remoting/client")

		val peerServlet = servletContext.addServlet("peerService", classOf[HttpRequestHandlerServlet])
		peerServlet.setLoadOnStartup(1)
		peerServlet.addMapping("/remoting/peer")
		servletContext.addFilter("securityFilter", SecurityFilter).addMappingForUrlPatterns(null, false, "/remoting/peer")
	}

}
